#-*- coding: utf-8 -*-
from models import BuidingSlot, GroundSlot

class AbstractAction(object):
    """
    The base class for all actions
    """
    __slots__ = ['x', 'y']

    def __init__(self, **kwargs):
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

    def execute():
        raise Exception("Abdsctract action cannot be executed")


class UpgrageBuildingAction(AbstractAction):
    """
    Building construction and upgrading
    """
    __slots__ = ['building_type']

    def execute():
        building = BuildingSlot.objects.get(x=self.x, y=self.y)
        building.building_type = self.buiding_type
        building.level += 1
        building.save()


class DestroyBuildingAction(AbstractAction):
    """
    Building destroying
    """
    __slots__ = ['damaged_levels']

    def execute():
        building = BuildingSlot.objects.get(x=self.x, y=self.y)
        if damaged_levels is None or damaged_levels >= building.level:
            building.buiding_type = EMPTY
            building.level = 0
        else:
            building.level -= damaged_levels
        building.save()


class UpgrageGroundAction(AbstractAction):
    """
    Resource mine construction and upgrading
    """
    def execute(self):
        ground = GroundSlot.objects.get(x=self.x, y=self.y)
        ground.level += 1
        ground.settlement.update_resources()
        ground.settlement.update_gather_speed()
        ground.settlement.save()
        ground.save()
     
   
class DestroyGroundAction(AbstractAction):
    """
    Resource mine destroying
    """
    __slots__ = ['damaged_levels']

    def execute(self):
        ground = GroundSlot.objects.get(x=self.x, y=self.y)
        if damaged_levels is None or damaged_levels >= ground.level:
            ground.level = 0
        else:
            ground.level -= damaged_levels
        ground.save()

